<?php

namespace app\api\controller;
use robot\Robot;
use think\Controller;
use think\Db;

class Weixin extends Controller
{
	
	public function index()
	{	

		if($this->checkSign()){
			if(input('echostr')){
				exit(input('echostr'));
			}else{
				// file_put_contents("./log.txt", date("Y-m-d H:i:s").json_encode(input(''))."\n",FILE_APPEND);
				$this->respon();
			}
		}else{
			file_put_contents("./error_log.txt", json_encode(input(''))."\n",FILE_APPEND);
		}
	}

	public function respon()
	{
		$postStr = $GLOBALS['HTTP_RAW_POST_DATA'];

		$this->log($postStr);

		$postObj = simplexml_load_string($postStr,'SimpleXMLElement', LIBXML_NOCDATA);

		$this->log($postObj->FromUserName);

		switch ($postObj->MsgType) {
			case 'text':
				$this->responText($postObj);
				break;
			case 'image':
				$this->responImage($postObj);
				break;
			case 'video':
				$this->responVideo($postObj);
				break;
			default:
				$this->responText($postObj);
				break;
		}

	}

	public function responText($postObj)
	{

		$res = Robot::send($postObj->Content);
		
		$str = "<xml><ToUserName><![CDATA[".$postObj->FromUserName."]]></ToUserName><FromUserName><![CDATA[".$postObj->ToUserName."]]></FromUserName><CreateTime>".time()."</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[".$res."]]></Content></xml>";

		exit($str);

		$musicInfo = $this->music($postObj->Content);
		

		$str = "<xml>
		 <ToUserName><![CDATA[".$postObj->FromUserName."]]></ToUserName>
		 <FromUserName><![CDATA[".$postObj->ToUserName."]]></FromUserName>
		 <CreateTime>".time()."</CreateTime>
		 <MsgType><![CDATA[music]]></MsgType>
		 <Music>
		 <Title><![CDATA[".$musicInfo['songinfo']['title']."]]></Title>
		 <Description><![CDATA[".$musicInfo['songinfo']['author']."]]></Description>
		 <MusicUrl><![CDATA[".$musicInfo['bitrate']['file_link']."]]></MusicUrl>
		 <HQMusicUrl><![CDATA[".$musicInfo['bitrate']['file_link']."]]></HQMusicUrl>
		 </Music>
		 </xml>";

        exit($str);

	}

	public function music($name='')
	{
		// $name = "伤心太平洋";

		$url = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.search.catalogSug&query=".$name;

		$data = file_get_contents($url);

		$data = json_decode($data,true);


		$songid = $data['song']['0']['songid'];

		$url = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.play&songid=".$songid;

		$data = file_get_contents($url);

		$data = json_decode($data,true);

		return $data;
	}

	public function log($str)
	{
		file_put_contents("./log.txt", date("Y-m-d H:i:s")."--".$str."\n",FILE_APPEND);
	}

	public function checkSign()
    {

    	return true;
    	$signature = input('signature');
        $timestamp = input('timestamp');
        $nonce = input('nonce');
        $token = "xbs";

        $tmpArr = array($token, $timestamp, $nonce);

        sort($tmpArr);

        $tmpStr = implode($tmpArr);

        $tmpStr = sha1($tmpStr);
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

}